package org.i_plus.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.i_plus.model.BlogBean;
import org.i_plus.model.MemberBean;
import org.i_plus.model.dao.MemberDAOHibernate;
import org.i_plus.model.services.BlogService;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class BlogServlet extends HttpServlet {
	private BlogService service ;
	private MemberDAOHibernate dao;

	@Override
	public void init() throws ServletException {
		ServletContext application = this.getServletContext();
		ApplicationContext context=
				WebApplicationContextUtils.getWebApplicationContext(application);
		this.service = (BlogService)context.getBean("blogService");
		this.dao = (MemberDAOHibernate)context.getBean("MemberDAO");
		super.init();
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//接收資料

			String temp1 = req.getParameter("account");

			String temp4 = req.getParameter("content");
			System.out.println("content:"+temp4);


			
		//驗證資料
			Map<String,String> errors = new HashMap<String,String>();
			req.setAttribute("errors", errors);
			
			
		//轉換資料
			
			
		//呼叫Model dispatch到新的view
			BlogBean blog = new BlogBean();
			MemberBean member = new MemberBean();
			member.setAccount(temp1);
			blog.setMemberBean(member);
			blog.setContent(temp4);
			
			String temp2 = blog.getMemberBean().getAccount();
			//MemberDAOHibernate dao = new MemberDAOHibernate();			
			blog.setMemberBean(dao.Select_By_Account(temp2));
			String name=blog.getMemberBean().getName();
			
			service.insertB(blog);
			
			
		
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doGet(req, resp);
	}
	


	
}
